/*****************************************************************************
 *  TouchMe
 *  Copyright (C) 2010  Daniel Lerch Hostalot <dlerch@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

#include "util.h"
#include "common.h"

point_t calib[9];

void read_conf()
{
	FILE *f = fopen(CONFFILE, "r");
	if(!f)
	{
		perror("fopen()");
		return;
	}

	int i=0;
	while((!feof(f))&&(i<9))
	{
		fscanf(f, "(%d, %d)\n", &calib[i].x, &calib[i].y);
		printf("%d-%d\n", calib[i].x, calib[i].y);
		i++;
	}

	fclose(f);

}

int map_x(int x, int y, int xmax)
{
	int kp = SCREEN/4;
	int xp = xmax/4;

	if(x<kp*1 && y<kp*1)
		return (x * xp*1) / calib[0].x;

	else if(x<kp*2 && y<kp*2)
		return (x * xp*2) / calib[3].x;

	return (x * xmax) / SCREEN;
}

int map_y(int x, int y, int ymax)
{	
	int kp = SCREEN/4;
	int xp = ymax/4;

	if(x<kp*1 && y<kp*1)
		return (y * xp*1) / calib[0].y;

	else if(x<kp*2 && y<kp*2)
		return (y * xp*2) / calib[1].y;

	return (y * ymax) / SCREEN;
}

int main(int argc, char **argv)
{
	read_conf();

	struct input_event ev;
	int x=0;
	int y=0;
	int press=0;
	int xmax=0;
	int ymax=0;
	int lx=0;
	int ly=0;
	int x_updated=0;
	int y_updated=0;
	int press_updated=0;
	int t_press = 0;

	Display *display = XOpenDisplay(getenv("DISPLAY"));

	get_screen_size(display, &xmax, &ymax);
	printf("screen size: %dx%d\n", xmax, ymax);


	if(argc!=2)
	{
		printf("Usage: %s [event file: /dev/input/event5]\n\n", argv[0]);
		return 0;
	}

	int fd=open(argv[1], O_RDONLY);
	if(fd<0)
	{
		perror("open()");
		return 0;
	}


	for (;;) 
	{
		int r=read(fd, &ev, sizeof(struct input_event));

		if (r==-1 && errno==EINTR) 
			continue;
		
		if (r==-1) 
			break;

		if(ev.type==3 && ev.code==4)
		{
			lx = ev.value;
			x = map_x(ev.value, ly, xmax);
			x_updated=1;
		}

		else if(ev.type==3 && ev.code==5)
		{
			ly = ev.value;
			y = map_y(lx, ev.value, ymax);
			y_updated=1;
		}

		else if(ev.type==1 && ev.code==272)
		{
			if(press==0 && ev.value==1)
			{
				press_updated=1;
			}
			else if(press==1 && ev.value==0)
			{
				printf("button released (%d, %d) (%d, %d)\n", x, y, lx, ly);
				mouse_button_release(display, Button1);
				enter_key(display);
				if(ev.time.tv_sec-t_press > 2)
				{
					//printf("click: %ld\n", ev.time.tv_sec-t_press);
					//mouse_button_press(display, 0);
					//usleep(100000);
					//mouse_button_release(display, 0);
				}
			}
			
			press=ev.value;
		}

		else
		{
			//printf("Event: time %ld.%06ld, type %d, code %d, value %d\n",
			//	ev.time.tv_sec, ev.time.tv_usec, ev.type, ev.code, ev.value);
		}
		
		if(y_updated && y_updated)
		{
			//printf("pointer (%d, %d) (%d, %d)\n", x, y, lx, ly);
			mouse_move(display, x, y);

			if(press_updated)
			{
				printf("button pressed (%d, %d) (%d, %d)\n", x, y, lx, ly);
				mouse_button_press(display, Button1);
				press_updated=0;
				t_press = ev.time.tv_sec;
			}
			x_updated = 0;
			y_updated = 0;
		}
	}
	perror("read");
	close(fd);

	XCloseDisplay(display);
	return 0;
}






